package com.ctg.itrdc.imageconsole.control.image;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.DMSReturnImageDTO;
import com.ctg.itrdc.imageconsole.data.dto.DockerImageFileDTO;
import com.ctg.itrdc.imageconsole.data.dto.DockerImageIndexDTO;
import com.ctg.itrdc.imageconsole.data.dto.ImagePushDTO;
import com.ctg.itrdc.imageconsole.service.IDockerAppImageService;
import com.ctg.itrdc.imageconsole.service.IDockerAppTypeService;
import com.ctg.itrdc.imageconsole.service.IDockerImageService;
import com.ctg.itrdc.imageconsole.service.IDockerImageIndexService;
import com.ctg.itrdc.imageconsole.service.IIndexService;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;

/**
 * 
 * @description: 镜像库相关
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年6月23日 下午8:57:49 Piao.Wu v1.0.0 create
 *
 */
@Controller
@RequestMapping("/imageRepository")
public class ImageRepositoryController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IDockerAppTypeService appTypeService;
	@Resource
	private IDockerImageService imageFileService;
	@Resource
	private IDockerImageIndexService imageIndexService;
	@Resource
	private IIndexService indexService;
	@Resource
	private IDockerAppImageService dockerAppImageService;


	@RequestMapping(value = "/queryImageList", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageList(@RequestParam(value = "imageName", required = false) String imageName,
			@RequestParam(value = "imageType", required = false) String imageType, @RequestParam("pageNum") int pageNum,
			@RequestParam("pageSize") int pageSize) {
		try {
			PageInfo<DockerImageFileDTO> pageInfo = imageFileService.queryImageFilePageInfo(imageName, imageType,
					pageNum, pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			PageInfo<DockerImageFileDTO> pageInfoForNull = new PageInfo<>();
			pageInfoForNull.setList(new ArrayList<DockerImageFileDTO>());

			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfoForNull);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}


	/**
	 * 
	 * @Description: 根据id查询镜像具体信息
	 * @author Piao.Wu 2016年6月24日 上午9:18:33
	 * @param imageFileId
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryImageInfo", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageInfo(@RequestParam("imageFileId") Long imageFileId) {
		try {
			DockerImageFileDTO imageFileDTO = imageFileService.FindByPrimaryKey(imageFileId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, imageFileDTO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	/**
	 * 
	 * @Description: 根据镜像id查询该镜像下的每个版本信息
	 * @author Piao.Wu 2016年6月24日 上午10:42:00
	 * @param imageFileId
	 * @param pageNum
	 * @param pageSize
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryPerVersionByImageId", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPerVersionByImageId(@RequestParam("imageFileId") Long imageFileId,
			@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
		try {
			PageInfo<DockerImageIndexDTO> pageInfo = imageIndexService.findByImageFileId(imageFileId, pageNum,
					pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			PageInfo<DockerImageIndexDTO> pageInfoForNull = new PageInfo<>();
			pageInfoForNull.setList(new ArrayList<DockerImageIndexDTO>());

			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfoForNull);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	/**
	 * 
	 * @Description: 构建成功后将信息保存到控制台的数据库(此接口提供给部署引擎的镜像构建功能那里使用)
	 * @author Piao.Wu
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/saveInfoAfterPush", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult saveInfoAfterPush(@RequestBody ImagePushDTO imagePushDTO) {
		try {
			logger.info("接受到一次saveInfoAfterPush请求!");
			String result = indexService.saveInfoAfterPush(imagePushDTO.getIpAndPort(), imagePushDTO.getTenantCode(),
					imagePushDTO.getImageName(), imagePushDTO.getDockerfile());
			if ("success".equalsIgnoreCase(result)) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, result);
			} else {
				return new CommonResult(ReturnCodeDefine.Code.C1102, result);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	
	@RequestMapping(value = "/microImages", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getImages(
			@RequestParam(value = "pageNum", required = false)  Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize)
	{
	
		Long userId = WebUtils.getCtgUser().getSysUserId();
		if(null == pageNum)
			pageNum = new Integer(1);
		if(null == pageSize)
			pageSize = new Integer(12); 
		try
		{
			PageInfo<DMSReturnImageDTO> pageInfo = dockerAppImageService.getImageFileDTO(userId, pageNum, pageSize);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
		} catch(Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null); 
		}		
	}
}
